#include <SoftwareSerial.h>

const byte rxPin = 9;
const byte txPin = 8;

SoftwareSerial mySerial (rxPin, txPin);

int Init_Finish=0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);  
  Serial.println("hello esp8266 begin");
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(115200);
  //delay(10000);
  Serial.println("wifi init");  
  WifiInit();
}

void WifiInit(void)
{
  SendCmd("AT+CWMODE=3\r");//设置为softAP+station共存模式
  delay(5000);
  SendCmd("AT+RST\r");//重启WIFI生效
  delay(50000);
  SendCmd("AT+CWJAP=\"HiWiFi_BaoBao\",\"198291dylhdylh\"\r");//连接无线网络
  delay(5000);
  SendCmd("AT+CIFSR\r");//返回设备的IP地址
  delay(5000);
  SendCmd("AT+CIPSTART=\"TCP\",\"192.168.199.128\",8234\r");//和目标服务器建立TCP连接
  delay(8000);
  SendCmd("AT+CIPMODE=1\r");//开启透传模式
  delay(5000);
  SendCmd("AT+CIPSEND\r");//开始透传
  delay(5000);
  SendCmd("hello world");
}

void SendCmd(String data)
{
  mySerial.println(data);  //发送AT指令 
}


void loop() {
  while(mySerial.available()>0) 
  {
      Serial.write(mySerial.read());  
  }
   SendCmd("hello world");
   delay(500);
}
